<template>
	<view>
		<tabbar currentText="我的">
			<view class="top">
				<view class="top_title">
					<!-- <image src="../../static/images/back.png" @click="to_back()" /> -->
					<text>我的</text>
				</view>
			</view>
			<view class="user_info">
				<image class="avatar" v-if="user_info['userAvatar']" @click="change_image()" :src="user_info['userAvatar']" />
				<image class="avatar_no_data" v-else @click="change_image()" src="../../static/images/mine_avatar.png" />
				<view class="user_name">{{user_info['userName']}}</view>
				<view class="user_phone">{{phone_show(user_info['userPhone'])}}</view>
			</view>
			<view class="content">
				<view class="user_menu" @click="to_url('/pages/mine/reset')">
					<image src="../../static/images/mine_edit_password.png" />
					<text>修改密码</text>
				</view>
			</view>
			<view class="logout" @click="logout()">退出系统</view>
		</tabbar>
	</view>
</template>

<script>
	import tabbar from "@/components/tabbar/tabbar.vue";
	import helper from '@/common/helper.js'
	export default {
		components: {
			tabbar
		},
		data() {
			return {
				focus: 0,
				show_type: 0,
				user_info: {}
			}
		},
		created() {
			this.user_info = uni.getStorageSync('user_info')
		},
		methods: {
			logout(){
				let self = this
				uni.showModal({
				    title: '提示',
				    content: '确定要退出系统？',
				    success: function (res) {
				        if (res.confirm) {
				            uni.clearStorageSync()
							if(self.user_info.userPhone) {
								uni.setStorageSync(self.user_info.userPhone, self.user_info.role)
							}
				            uni.reLaunch({
				            	url: '/pages/login/index'
				            })
				        } else if (res.cancel) {
				        }
				    }
				});
			},
			to_back() {
				uni.navigateBack()
			},
			phone_show(phone) {
				return phone ? phone.substr(0, 3) + ' ' + phone.substr(3, 4) + ' ' + phone.substr(7, 4) : ''
			},
			to_url(url) {
				uni.navigateTo({
					url: url
				})
			},
			change_image() {
				uni.chooseImage({
					success: (chooseImageRes) => {
						uni.uploadFile({
							url: helper.baseUrl + '/logon/user/editToC',
							filePath: chooseImageRes.tempFilePaths[0],
							name: 'file',
							header: {
								token: uni.getStorageSync('token')
							},
							formData: {
								'userId': this.user_info['userId']
							},
							success: (res) => {
								let obj = JSON.parse(res.data)
								this.user_info['userAvatar'] = obj.data
								this.user_info = JSON.parse(JSON.stringify(this.user_info))
								uni.setStorageSync('user_info', this.user_info)
								uni.showToast({
									title: '修改成功',
									icon: 'none'
								})
							}
						});
					}
				});
			}
		}
	}
</script>

<style scoped>
	.logout {
		width: 90%;
		height: 96rpx;
		line-height: 92rpx;
		color: #3B86FF;
		text-align: center;
		background: #FFFFFF;
		border: 2rpx solid #3B86FF;
		border-radius: 8rpx;
		position: fixed;
		box-sizing: border-box;
		left: 0;
		right: 0;
		margin: auto;
		bottom: 200rpx;
		font-weight: 400;
		font-size: 32rpx;
	}

	.content {
		padding: 0 30rpx;
		padding-top: 110rpx;
	}

	.user_menu {
		border-bottom: 1rpx solid #F2F2F2;
		height: 106rpx;
		line-height: 106rpx;
		font-weight: 500;
		font-size: 32rpx;
		color: #43425D;
	}

	.user_menu image {
		width: 32rpx;
		height: 32rpx;
		margin-left: 62rpx;
		margin-top: 40rpx;
	}

	.user_menu text {
		margin-left: 20rpx;
	}

	.user_name {
		font-size: 40rpx;
		height: 40rpx;
		line-height: 40rpx;
		font-weight: 500;
		margin-top: 82rpx;
	}

	.user_phone {
		height: 40rpx;
		font-size: 40rpx;
		font-weight: 500;
		margin-top: 32rpx;
	}

	.avatar_no_data {
		width: 184rpx;
		height: 200rpx;
		border-radius: 10rpx;
		display: block;
		margin: 0 auto;
	}

	.avatar{
		width: 200rpx;
		height: 200rpx;
		border-radius: 50%;
		display: block;
		margin: 0 auto;
	}

	.user_info {
		width: 650rpx;
		height: 548rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 10rpx 30rpx rgba(116, 169, 255, 0.55);
		margin: 0 auto;
		margin-top: -326rpx;
		padding-top: 74rpx;
		box-sizing: border-box;
		color: #43425D;
		text-align: center;
		border-radius: 20rpx;
	}

	.top {
		height: 562rpx;
		background: url(../../static/images/user_bg.png);
		background-size: cover;
		padding-top: calc(var(--status-bar-height) + 88rpx);
		box-sizing: border-box;
	}

	.top_title {
		color: #fff;
		height: 32rpx;
		line-height: 32rpx;
		font-size: 32rpx;
		text-align: center;
		position: relative;
		font-weight: 400;
	}

	.top_title image {
		width: 21rpx;
		height: 37rpx;
		position: absolute;
		left: 34rpx;
		top: -5rpx;
	}
</style>
